<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/17/21
 * Time: 10:31 PM
 */

namespace app\api\controller\v1;


use app\api\model\ProductOrder;
use app\api\model\UserInvitation;
use app\api\model\User;
use app\api\model\StoreUser;
use app\api\model\TeamUser;
use LinCmsTp5\exception\ParameterException;

class Bus
{
    // 获取全部信息
    public function getDataAll($store_id,$name,$start_time,$end_time)
    {
        $where[] = ['status','gt',1];
        $where[] = ['bus_status','eq',2];
        if($store_id) {
            $where[] = ['store_id','eq',$store_id];
        }
        if($name) {
            $where[] = ['out_trade_no','eq',$name];
        }
        if($start_time) {
            $where[] = ['pay_time','egt',strtotime($start_time)];
            $where[] = ['pay_time','elt',strtotime($end_time)];
        }
        return ProductOrder::where($where)->order('pay_time desc')->with('store,user')->select()->each(function ($item){
            $invite = UserInvitation::where('invitee',$item['user_id'])->find();
            if($invite['type'] == 1) {
                $item['invite'] = User::get($invite['inviter']);
            }elseif($invite['type'] == 2){
                $store_user = StoreUser::get($invite['inviter']);
                $item['invite'] = User::get($store_user['user_id']);
                $item['invite']['nickname'] = $store_user['name'];
                $item['invite']['phone'] = $store_user['phone'];
            }elseif($invite['type'] == 3){
                $team_user = TeamUser::get($invite['inviter']);
                $item['invite'] = User::get($team_user['user_id']);
                $item['invite']['nickname'] = $team_user['name'];
                $item['invite']['phone'] = $team_user['phone'];
            }
            if($item['invitation_money'] > $item['actual_price']) {
                $item['invitation_money'] = $item['invitation_money']/2;
            }
            $item['pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
        })->toArray();
    }

    /**
     * 获取核销信息
     * @validate('PagingParameter')
     */
    public function getDataList($store_id,$name,$page,$limit,$start_time,$end_time)
    {
        $where[] = ['status','gt',1];
        $where[] = ['bus_status','eq',2];
        if($store_id) {
            $where[] = ['store_id','eq',$store_id];
        }
        if($name) {
            $where[] = ['out_trade_no','eq',$name];
        }
        if($start_time) {
            $where[] = ['pay_time','egt',strtotime($start_time)];
            $where[] = ['pay_time','elt',strtotime($end_time)];
        }
        $query = ProductOrder::where($where)->order('pay_time desc')->with('store,user');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            $invite = UserInvitation::where('invitee',$item['user_id'])->find();
            if($invite['type'] == 1) {
                $item['invite'] = User::get($invite['inviter']);
            }elseif($invite['type'] == 2){
                $store_user = StoreUser::get($invite['inviter']);
                $item['invite'] = User::get($store_user['user_id']);
                $item['invite']['nickname'] = $store_user['name'];
                $item['invite']['phone'] = $store_user['phone'];
            }elseif($invite['type'] == 3){
                $team_user = TeamUser::get($invite['inviter']);
                $item['invite'] = User::get($team_user['user_id']);
                $item['invite']['nickname'] = $team_user['name'];
                $item['invite']['phone'] = $team_user['phone'];
            }
            if($item['invitation_money'] > $item['actual_price']) {
                $item['invitation_money'] = $item['invitation_money']/2;
            }
            $item['pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    // 导入信息
    public function updateData($excel_data)
    {
        if(count($excel_data) > 500) {
            throw new ParameterException([
                'msg' => [
                    'bus' => '最多导入500条数据'
                ]
            ]);
        }
        if(count($excel_data) == 0) {
            throw new ParameterException([
                'msg' => [
                    'bus' => '导入信息为空'
                ]
            ]);
        }
        foreach ($excel_data as $k=>$v){
            $info = ProductOrder::where('out_trade_no',$v['out_trade_no'])->find();
            if($info['bus_msg'] != 'REFUND') {
                ProductOrder::where('out_trade_no',$v['out_trade_no'])->update([
                    'bus_price' => $v['bus_price'],
                    'bus_status' => 2,
                    'bus_time' => time(),
                    'bus_msg' => $v['bus_msg']
                ]);
            }

        }
        return writeJson(202, [], '导入成功！');
    }
}